Not a good idea. This would prevent the system from crashing, but probably hides some other serious problem with the input data.
Here is a complete program that uses the factorial()
//FactorialTester.java
//
class FactorialCalc
{
int fact( int N )
{
if ( N == 0 )
return 1;
else
return N * fact( N-1 ) ;
}
}
class FactorialTester
{
public static void main ( String[] args)
{
int argument = 10;
FactorialCalc f = new FactorialCalc();
int result = f.fact( argument );
System.out.println("Factorial(" + argument + ") is " + result );
}
}
Here is a run of the program:
C:\JavaNotes\Recursion03>javac FactorialTester.java C:\JavaNotes\Recursion03>java FactorialTester Factorial(10) is 3628800